<script setup>
// import { GetSalesTotalListAPI } from '@/api/statistics/procure/orderTack.js'

import { GetCustomSelsctAPI, GetCkAPI } from '@/api/common/index'
const { proxy } = getCurrentInstance()
const { statistics_procure_order_track_state } = proxy.useDict('statistics_procure_order_track_state')

// 列表数据
const tableData = ref([])

// 按钮
const buttonList = ref([{ type: 'export', hasPermi: 'finance:dataChange:export' }])

// 列表枚举
const column = ref([
  { label: '客户编码', prop: 'customerCode', width: '180' },
  { label: '客户名称', prop: 'customerName' },
  { label: '物料编码', prop: 'materialCode', width: '180' },
  { label: '物料名称', prop: 'materialName', width: '180' },
  { label: '类别', prop: 'categoryDict', width: '180' },
  { label: '物料类别', prop: 'materialCategory' },
  { label: '规格', prop: 'materialSpec' },
  { label: '辅助属性', prop: 'auxiliaryAttribute' },
  { label: '仓库', prop: 'warehouseDict' },
  { label: '多单位', prop: 'materialMoreUnit' },
  { label: '多单位数量', prop: 'materialMoreUnitNumber' },
  { label: '单位', prop: 'unitDict' },
  { label: '单位数量', prop: 'materialTotal' },
  { label: '单价', prop: 'unitPrice' },
  { label: '销售金额', prop: 'saleAmount' },
  { label: '销售成本', prop: 'saleCost' },
  { label: '销售毛利', prop: 'saleGrossProfit' },
  { label: '毛利率', prop: 'grossProfitMargin' }
])

// 状态
const state = reactive({
  treeLoading: false,
  loading: false, // 列表加载状态
  btnLoading: false, // 按钮加载状态
  isShow: false, // 弹窗状态
  title: '新增', // 弹窗标题
  total: 0 // 列表总量
})

// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10
})

// 查询条件 枚举
const selectList = ref([
  { type: '日期范围' },
  {
    label: '客户类别',
    model: 'customerCategory',
    type: '下拉单选',
    opt: []
  },
  { label: '客户', model: 'customerName', type: '输入框' },
  { label: '物料名称', model: 'materialName', type: '输入框' },
  {
    label: '仓库',
    model: 'warehouseNameId',
    type: '下拉单选',
    opt: []
  }
])

// 导出
const exportFun = () => {}

// 查询条件
const selectSubmit = val => {
  Object.keys(val).forEach(keyItem => (query[keyItem] = val[keyItem]))
  getList()
}

// 获取列表
const getList = async () => {
  query.statisticalMethod = 2
  state.loading = true
  try {
    const result = await GetSalesTotalListAPI(query)
    tableData.value = result.rows
    state.total = result.total
  } catch {
  } finally {
    state.loading = false
  }
}
getList()

// 获取客户下拉数据
const getcustomerSelsct = async () => {
  const { data: res } = await GetCustomSelsctAPI()

  selectList.value[1].opt = res
}
getcustomerSelsct()

// 获取仓库
const getCkById = async () => {
  const res = await GetCkAPI()
  selectList.value[4].opt = res.data.map(item => ({
    label: item.warehouseName,
    value: item.id
  }))
}
getCkById()
</script>

<template>
  <div class="app-container">
    <!-- 查询条件组件 -->
    <p-select-form :selectForm="selectList" @selectSubmit="selectSubmit" />
    <el-card>
      <!-- 按钮组件 -->
      <p-button :list="buttonList" @add="addDic" />
      <!-- 表格数据 -->
      <p-table
        class="tableClass"
        summary
        :amountList="['materialTotal', 'saleAmount']"
        :loading="state.loading"
        :isSel="false"
        :data="tableData"
        :column="column"
        :total="state.total"
        @change="handleSelectionChange"
        @pagination="getList"
        v-model:page="query.pageNum"
        v-model:limit="query.pageSize"
      >
        <template #columnCell="{ column, record }">
          <p-dictTag
            v-if="column.label == '单据状态'"
            :options="statistics_procure_order_track_state"
            :value="record.status"
          />
          <!-- 辅助属性 -->
          <p-producer :data="record.auxiliaryAttribute" v-if="column.label == '辅助属性'" />
        </template>
      </p-table>
    </el-card>
  </div>
</template>
<style scoped lang="scss"></style>
